package kr.hkit.powerjava.chapter26.Young2;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Scanner;

public abstract class AbsService<T> {

	protected DAOInterface<T> dao;
	protected Scanner sc;
	protected Connection connection;

	public AbsService(Scanner sc, Connection connection) {
		this.sc = sc;
		this.connection = connection;
	}

	public abstract T inputItemInfo(boolean gubun);

	public abstract void prnSubMenu();

	public void prnMenu() {

		int res = 0;
		T item = null;

		do {
			System.out.println("1.입력 2.삭제 3.수정 4.검색 5.리스트보기 6.종료");
			res = sc.nextInt();
			try {
				switch (res) {
					case 1:
						System.out.println("삽입할 정보를 입력하시오.");
						item = inputItemInfo(true);
						dao.insertItem(item);
						break;
					case 2:
						System.out.println("삭제할 정보를 입력하시오.");
						item = inputItemInfo(false);
						dao.deleteItem(item);
						break;
					case 3:
						System.out.println("수정할 정보를 입력하시오.");
						item = inputItemInfo(true);
						dao.updateItem(item);
						break;
					case 4:
						System.out.println("검색할 정보를 입력하시오.");
						item = inputItemInfo(false);
						item = dao.searchItem(item);
						System.out.println(item); // 콘솔에 출력
						break;
					case 5:
						prnSubMenu();
						break;
					default:
						res = 7;
						break;
				}

			} catch (SQLException e) {
				e.printStackTrace();
			}

		} while (res <= 6);

	}
	
	
	public void prnItemList(Comparator<T> comparator, ArrayList<T> list) {
		Collections.sort(list, comparator);
		
		for (T d : list) {
			System.out.println(d);
		}
	}
}
